
var options = {
	lines: { show: true },
	points: { show: true },
	xaxis: { tickDecimals: 0, tickSize: 1 }
    };
var data = [];
var placeholder_legend;
var placeholder_2;
var placeholder;
// fetch one series, adding to what we got
var alreadyFetched = {};

var EMPTY = new Array();
var DS18B20_A = new Array();
var DS18B20_B = new Array();
var AM2302_RH = new Array();
var legend_date_time = "";
var legend_DS18B20_A = "";
var legend_DS18B20_B = "";
var legend_AM2302_RH = "";
var iteration = 0;
var get_proto_host = window.location.protocol + "//" + location.hostname;
// strings
var Date_Time = "Date & Time";
var Indoor_Air_Temperature = "Indoor Air Temperature";
var Outdoor_Air_Temperature = "Outdoor Air Temperature";
var Outdoor_Relative_Humidity = "Outdoor Relative Humidity";
var Temp = "Temp"
var Humidity = "Humidity"

function MyInit(plot1, plot2, plot3, Language) {
	if (Language == 1) // is Chinese
	{
		Date_Time = "\u65f6\u95F4"; // 时间
		Indoor_Air_Temperature = "\u5BA4\u5185\u6C14\u6E29"; // 室内气温
		Outdoor_Air_Temperature = "\u5BA4\u5916\u6C14\u6E29"; // 室外气温
		Outdoor_Relative_Humidity = "\u5BA4\u5916\u76F8\u5BF9\u6E7F\u5EA6"; // 室外相对湿度
		Temp = "\u6C14\u6E29" // 气温
		Humidity = "\u6E7F\u5EA6" // 湿度
	}

	placeholder_legend = $(plot1);
	placeholder_2 = $(plot2);
	placeholder = $(plot3);
	$.plot(placeholder_legend, data, options);
	$.plot(placeholder, data, options);
	$.plot(placeholder_2, data, options);

	// reset data
	data = [];
	alreadyFetched = {};

	$.plot(placeholder, data, options);
	$.plot(placeholder_2, data, options);
	$.plot(placeholder_legend, data, options);

	fetchData();
}

function fetchData() {
		function onDataReceived(series) {
			// we get all the data in one go, if we only got partial
            // data, we could merge it with what we already got
            data = [ series ];
            var RcvData = series;
            var date_time;

            	date_time = Date.parse(RcvData.charAt( 0 ) + RcvData.charAt( 1 ) + RcvData.charAt( 2 ) + RcvData.charAt( 3 )
                		+ "/" + RcvData.charAt( 5 ) + RcvData.charAt( 6 ) + "/" + RcvData.charAt( 8 ) + RcvData.charAt( 9 )
                		+ " " + RcvData.charAt( 11 ) + RcvData.charAt( 12 ) + ":" + RcvData.charAt( 14 ) + RcvData.charAt( 15 )
                		+ ":" + RcvData.charAt( 17 ) + RcvData.charAt( 18 )) + 28800000;

                legend_date_time = RcvData.charAt( 0 ) + RcvData.charAt( 1 ) + RcvData.charAt( 2 ) + RcvData.charAt( 3 )
                		+ "-" + RcvData.charAt( 5 ) + RcvData.charAt( 6 ) + "-" + RcvData.charAt( 8 ) + RcvData.charAt( 9 )
                		+ "&nbsp;" + RcvData.charAt( 11 ) + RcvData.charAt( 12 ) + ":" + RcvData.charAt( 14 ) + RcvData.charAt( 15 )
                		+ ":" + RcvData.charAt( 17 ) + RcvData.charAt( 18 );

            	if (RcvData.charAt( 30 ) == '1')  // CRC error
            	{
            		DS18B20_A.push( new Array(null, null));
            		legend_DS18B20_A = "n/a";
            	}
            	else
            	{
            		DS18B20_A.push( new Array(date_time, RcvData.charAt( 22 ) + RcvData.charAt( 23 ) + RcvData.charAt( 24 ) + RcvData.charAt( 25 ) + RcvData.charAt( 26 )
            	    		 + RcvData.charAt( 27 ) + RcvData.charAt( 28 ) + RcvData.charAt( 29 )));

            	   	if (RcvData.charAt( 23 ) == '0')
            	   		legend_DS18B20_A = RcvData.charAt( 24 ) + RcvData.charAt( 25 ) + RcvData.charAt( 26 )
            	    		 + RcvData.charAt( 27 ) + RcvData.charAt( 28 ) + RcvData.charAt( 29 );
            	    else
            	    	legend_DS18B20_A = RcvData.charAt( 23 ) + RcvData.charAt( 24 ) + RcvData.charAt( 25 ) + RcvData.charAt( 26 )
            	    		 + RcvData.charAt( 27 ) + RcvData.charAt( 28 ) + RcvData.charAt( 29 );
            	    
            	    if (RcvData.charAt( 22 ) == '-')
            	    	legend_DS18B20_A = Number('-' + String(legend_DS18B20_A));
           		}

            	if (RcvData.charAt( 39 ) == '1')  // CRC error
            	{
            		DS18B20_B.push( new Array(null, null));
            		legend_DS18B20_B = "n/a";
            	}
            	else
            	{
            		DS18B20_B.push( new Array(date_time, RcvData.charAt( 31 ) + RcvData.charAt( 32 ) + RcvData.charAt( 33 ) + RcvData.charAt( 34 ) + RcvData.charAt( 35 )
            	    		 + RcvData.charAt( 36 ) + RcvData.charAt( 37 ) + RcvData.charAt( 38 )));

            	    if (RcvData.charAt( 32 ) == '0')
            	    	legend_DS18B20_B = RcvData.charAt( 33 ) + RcvData.charAt( 34 ) + RcvData.charAt( 35 )
            	    		 + RcvData.charAt( 36 ) + RcvData.charAt( 37 ) + RcvData.charAt( 38 );
            	    else
            	    	legend_DS18B20_B = RcvData.charAt( 32 ) + RcvData.charAt( 33 ) + RcvData.charAt( 34 ) + RcvData.charAt( 35 )
            	    		 + RcvData.charAt( 36 ) + RcvData.charAt( 37 ) + RcvData.charAt( 38 );

            	    if (RcvData.charAt( 31 ) == '-')
            	    	legend_DS18B20_B = Number('-' + String(legend_DS18B20_B));
            	}

            	if (RcvData.charAt( 50 ) == '1')  // checksum error
            	{
            		AM2302_RH.push( new Array(null, null));
            		legend_AM2302_RH = "n/a"
            	}
            	else
            	{
            		AM2302_RH.push( new Array(date_time, RcvData.charAt( 46 ) + RcvData.charAt( 47 ) + RcvData.charAt( 48 ) + RcvData.charAt( 49 )));
            		if (RcvData.charAt( 46 ) == '0')
            			legend_AM2302_RH = RcvData.charAt( 47 ) + RcvData.charAt( 48 ) + RcvData.charAt( 49 );
            		else
            			legend_AM2302_RH = RcvData.charAt( 46 ) + RcvData.charAt( 47 ) + RcvData.charAt( 48 ) + RcvData.charAt( 49 );
            	}

            $.plot($("#placeholder_legend"),
           		[ { data: EMPTY, label: Date_Time + ":&nbsp;&nbsp;" + legend_date_time},
           		{ data: EMPTY, label: Indoor_Air_Temperature + "(&deg;C):&nbsp;" + legend_DS18B20_A },
           		{ data: EMPTY, label: Outdoor_Air_Temperature + "(&deg;C):&nbsp;" + legend_DS18B20_B },
             	{ data: EMPTY, label: Outdoor_Relative_Humidity + "(%):&nbsp;" + legend_AM2302_RH }],
           		{
             		lines: { show: false },
             		colors: ["#599cd4", "#edc240", "#edc240", "#afd8f8"],
             		grid: { show: false },
             		points: { show: false },
             		legend: { position: 'sw', margin: 0} });

            $.plot($("#placeholder"),
           		[ { data: DS18B20_A, label: Temp + "(&deg;C):&nbsp;" + legend_DS18B20_A} ],
           		{
           			xaxis: { mode: 'time', twelveHourClock: true },
             		yaxis: { tickFormatter: function (v, axis) { return v.toFixed(axis.tickDecimals) + " °C"} },
             		legend: { position: 'sw' } });

        	$.plot($("#placeholder_2"),
           		[ { data: DS18B20_B, label: Temp + "(&deg;C):&nbsp;" + legend_DS18B20_B },
             	{ data: AM2302_RH, label: Humidity + "(%):&nbsp;" + legend_AM2302_RH, yaxis: 2 }],
           		{
             		xaxes: [{ mode: 'time', twelveHourClock: true }],
             		yaxes: [{ tickFormatter: function (v, axis) { return v.toFixed(axis.tickDecimals) + " °C"} }, { alignTicksWithAxis: 1, position: "right", tickFormatter: function (v, axis) { return v.toFixed(1) + " %"}} ],
             		legend: { position: 'sw' } });
		}

		function OnErrorOccurred(series) {
			DS18B20_A.push( new Array(null, null));
            DS18B20_B.push( new Array(null, null));
            AM2302_RH.push( new Array(null, null));
        }

        var weblink;
       	weblink = get_proto_host + "/data.cgi";

        $.ajax({
        	// usually, we'll just call the same URL, a script
            // connected to a database, but in this case we only
            // have static example files so we need to modify the
            // URL
            url: weblink,
            method: 'GET',
            dataType: 'text',
            cache: true,
            success: onDataReceived,
            error: OnErrorOccurred
        });

        if (iteration < 720) // 720 * 5 == 1 hour
        {
        	++iteration;
        }
        else
        {
        	DS18B20_A.shift();
            DS18B20_B.shift();
            AM2302_RH.shift();
        }
	setTimeout(fetchData, 5000);
}
